package com.ff.miaosha.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ff.miaosha.access.AccessLimit;
import com.ff.miaosha.domain.Goods;
import com.ff.miaosha.domain.MiaoshaUser;
import com.ff.miaosha.redis.GoodsKey;
import com.ff.miaosha.redis.RedisService;
import com.ff.miaosha.result.Result;
import com.ff.miaosha.service.GoodsService;
import com.ff.miaosha.service.MiaoshaUserService;
import com.ff.miaosha.vo.GoodsDetailVo;
import com.ff.miaosha.vo.GoodsVo;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @RequestMapping("/to_list")
    public String list(Model model, MiaoshaUser user){
        model.addAttribute("user",user);
        //查询商品列表
        List<GoodsVo> goodsList=null;
        //fastjson 不能直接转换 List<Object>类型
        String cacheGoodsList=redisService.get(GoodsKey.getGoodsList,"",String.class);
        if (StringUtils.isNoneEmpty(cacheGoodsList)){
            goodsList= JSON.parseArray(cacheGoodsList,GoodsVo.class);
        }
        if (goodsList==null || goodsList.size()<=0){
            goodsList=goodsService.listGoodsVo();
            redisService.set(GoodsKey.getGoodsList,"",goodsList);
        }
        model.addAttribute("goodsList",goodsList);
        return "goods_list";
    }


    /*@RequestMapping("/to_detail/{goodsId}")
    public String detail(Model model, MiaoshaUser user, @PathVariable("goodsId") long goodsId){
        model.addAttribute("user",user);

        GoodsVo goods=goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods",goods);

        long startAt=goods.getStartDate().getTime();
        long endAt=goods.getEndDate().getTime();
        long now=System.currentTimeMillis();

        int miaoshaStatus=0;
        int remainSeconds=0;
        if (now <startAt){//秒杀还没开始，倒计时
            miaoshaStatus=0;
            remainSeconds= (int) ((startAt-now)/1000);
        }else if (now >endAt){//秒杀已经结束
            miaoshaStatus=2;
            remainSeconds=-1;
        }else{ //秒杀进行中
            miaoshaStatus=1;
            remainSeconds=0;
        }
        model.addAttribute("miaoshaStatus",miaoshaStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        return "goods_detail";
    }*/

    @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    @AccessLimit(seconds = 5,maxCount = 5)
    public Result<GoodsDetailVo> detail2(MiaoshaUser user, @PathVariable("goodsId") long goodsId){
        GoodsVo goods=null;
        goods=redisService.get(GoodsKey.getGoodsDetail,""+goodsId,GoodsVo.class);
        if (goods==null){
            goods=goodsService.getGoodsVoByGoodsId(goodsId);
            redisService.set(GoodsKey.getGoodsDetail,""+goodsId,goods);
        }

        long startAt=goods.getStartDate().getTime();
        long endAt=goods.getEndDate().getTime();
        long now=System.currentTimeMillis();

        int miaoshaStatus=0;
        int remainSeconds=0;
        if (now <startAt){//秒杀还没开始，倒计时
            miaoshaStatus=0;
            remainSeconds= (int) ((startAt-now)/1000);
        }else if (now >endAt){//秒杀已经结束
            miaoshaStatus=2;
            remainSeconds=-1;
        }else{ //秒杀进行中
            miaoshaStatus=1;
            remainSeconds=0;
        }
        GoodsDetailVo vo = new GoodsDetailVo();
        vo.setGoods(goods);
        vo.setUser(user);
        vo.setRemainSeconds(remainSeconds);
        vo.setMiaoshaStatus(miaoshaStatus);
        return Result.success(vo);
    }
}
